package com.kingmed.kmss.common.service.obs;

import com.kingmed.kmss.common.exception.BusinessException;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: 华为云OBS配置类
 */
@Data
@Slf4j
@Configuration
public class HuaWeiOBSConfig {

    /**
     * 访问密钥AK
     */
    @Value("${huaweiyun.obs.accessKey}")
    private String accessKey;

    /**
     * 访问密钥SK
     */
    @Value("${huaweiyun.obs.securityKey}")
    private String securityKey;

    /**
     * 终端节点
     */
    @Value("${huaweiyun.obs.endPoint}")
    private String endPoint;

    /**
     * 桶
     */
    @Value("${huaweiyun.obs.bucketName}")
    private String bucketName;

    /**
     * @Description 获取OBS客户端实例
     * @return: com.obs.services.ObsClient
     */
    public ObsClient getInstance() {
        return new ObsClient(accessKey, securityKey, endPoint);
    }

    /**
     * @Description 销毁OBS客户端实例
     */
    public void destroy(ObsClient obsClient) {
        try {
            obsClient.close();
        } catch (ObsException e) {
            log.error("obs执行失败", e);
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            log.error("执行失败", e);
            throw new BusinessException(e.getMessage());
        }
    }
}
